package com.baitai.system.service.impl;

import java.util.List;

import com.baitai.common.core.domain.entity.SysUser;
import com.baitai.common.core.redis.RedisCache;
import com.baitai.common.utils.DateUtils;
import com.baitai.system.domain.IssueTask;
import com.baitai.system.domain.UserPointsLog;
import com.baitai.system.domain.vo.ReceiveTaskVo;
import com.baitai.system.mapper.IssueTaskMapper;
import com.baitai.system.mapper.SysUserMapper;
import com.baitai.system.service.IUserPointsLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.baitai.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baitai.system.domain.ReceiveTaskFile;
import com.baitai.system.mapper.ReceiveTaskMapper;
import com.baitai.system.domain.ReceiveTask;
import com.baitai.system.service.IReceiveTaskService;

/**
 * 接取任务关系Service业务层处理
 * 
 * @author xx
 * @date 2024-07-20
 */
@Service
public class ReceiveTaskServiceImpl implements IReceiveTaskService 
{
    @Autowired
    private ReceiveTaskMapper receiveTaskMapper;
    @Autowired
    private IssueTaskMapper issueTaskMapper;
    @Autowired
    private IUserPointsLogService userPointsLogService;

    /**
     * 查询接取任务关系
     * 
     * @param id 接取任务关系主键
     * @return 接取任务关系
     */
    @Override
    public ReceiveTaskVo selectReceiveTaskById(Long id)
    {
        return receiveTaskMapper.selectReceiveTaskVoById(id);
    }

    /**
     * 查询接取任务关系列表
     * 
     * @param receiveTask 接取任务关系
     * @return 接取任务关系
     */
    @Override
    public List<ReceiveTask> selectReceiveTaskList(ReceiveTask receiveTask)
    {
        return receiveTaskMapper.selectReceiveTaskList(receiveTask);
    }

    /**
     * 新增接取任务关系
     * 
     * @param receiveTask 接取任务关系
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReceiveTask(ReceiveTask receiveTask)
    {
        receiveTask.setCreateTime(DateUtils.getNowDate());
        int rows = receiveTaskMapper.insertReceiveTask(receiveTask);
        insertReceiveTaskFile(receiveTask);
        return rows;
    }

    /**
     * 修改接取任务关系
     * 
     * @param receiveTask 接取任务关系
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReceiveTask(ReceiveTask receiveTask)
    {
        receiveTask.setUpdateTime(DateUtils.getNowDate());
        receiveTaskMapper.deleteReceiveTaskFileByReceiveTaskId(receiveTask.getId());
        insertReceiveTaskFile(receiveTask);
        return receiveTaskMapper.updateReceiveTask(receiveTask);
    }


    /**
     * 修改接取任务关系
     *
     * @param receiveTask 接取任务关系
     * @return 结果
     */
    @Transactional
    @Override
    public int auditReceiveTask(Long userId ,ReceiveTask receiveTask)
    {
        receiveTask.setUpdateTime(DateUtils.getNowDate());
        if(receiveTask.getAuditStatus().equals("1")){
            IssueTask issueTask = issueTaskMapper.selectIssueTaskById(receiveTask.getTaskId());
            userPointsLogService.gainPoints(userId,issueTask.getTaskUnitPrice().toString(),"1","完成任务");
        }
        return receiveTaskMapper.updateReceiveTask(receiveTask);
    }

    /**
     * 批量删除接取任务关系
     * 
     * @param ids 需要删除的接取任务关系主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReceiveTaskByIds(Long[] ids)
    {
        receiveTaskMapper.deleteReceiveTaskFileByReceiveTaskIds(ids);
        return receiveTaskMapper.deleteReceiveTaskByIds(ids);
    }

    /**
     * 删除接取任务关系信息
     * 
     * @param id 接取任务关系主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReceiveTaskById(Long id)
    {
        receiveTaskMapper.deleteReceiveTaskFileByReceiveTaskId(id);
        return receiveTaskMapper.deleteReceiveTaskById(id);
    }

    /**
     * 新增任务提交文件表信息
     * 
     * @param receiveTask 接取任务关系对象
     */
    public void insertReceiveTaskFile(ReceiveTask receiveTask)
    {
        List<ReceiveTaskFile> receiveTaskFileList = receiveTask.getReceiveTaskFileList();
        Long id = receiveTask.getId();
        if (StringUtils.isNotNull(receiveTaskFileList))
        {
            List<ReceiveTaskFile> list = new ArrayList<ReceiveTaskFile>();
            for (ReceiveTaskFile receiveTaskFile : receiveTaskFileList)
            {
                receiveTaskFile.setReceiveTaskId(id);
                list.add(receiveTaskFile);
            }
            if (list.size() > 0)
            {
                receiveTask.setAuditStatus("2");
                receiveTaskMapper.batchReceiveTaskFile(list);
            }
        }
    }


    /**
     * 新增接取任务关系
     *
     * @param receiveTask 接取任务关系
     * @return 结果
     */
    @Transactional
    @Override
    public int submitReceiveTask(Long userId,ReceiveTask receiveTask)
    {
        ReceiveTask receiveTaskRelation = receiveTaskMapper.selectReceiveTaskByUserIdAndTaskId(userId,receiveTask.getTaskId());
        if(receiveTaskRelation!=null){
            receiveTaskRelation.setReceiveTaskFileList(receiveTask.getReceiveTaskFileList());
            insertReceiveTaskFile(receiveTaskRelation);
            receiveTaskMapper.updateReceiveTask(receiveTaskRelation);
        }
        return 1;
    }
}
